
do "...\First.do"

** Find cases of parental illness among doctors- used in a robustness check and
** merged on later (7d_parental_illness.do)

** Merge mothers and fathers'cause of death and medication use

********************************************************************************
clear
use "$work\Yder_9518_long.dta"


keep ydernr pnr year mor_id far_id
duplicates drop 

** mother cause of deaht
rename pnr PCP_id
rename mor_id pnr 

merge m:1 pnr using "$work\deaths.dta"
drop if _merge==2

drop d_statdato
rename yod mom_yod

rename death* mom_death*
drop _merge

foreach var of varlist mom_death_CVC-mom_death_COPD {
    replace `var'=0 if `var'==. & pnr!=""
	replace `var'=0 if `var'!=. & pnr!="" & year<mom_yod
		
	}
tab mom_yod, m




** mom medication

preserve

keep pnr year
duplicates drop
drop if pnr==""

merge 1:m pnr year using "$work\lmdb_new_types.dta"
drop if _merge==2
drop _merge


duplicates drop

save "$work\yder_mom_meds.dta", replace

restore


merge m:1 pnr year using "$work\yder_mom_meds.dta"
drop _merge

foreach var of varlist statins ACE metformin COPD_med  {
 	rename `var' mom_`var'
	replace mom_`var'=0 if pnr!="" & mom_`var'==.
	replace mom_`var'=. if pnr==""

}



*** DADS
rename pnr mor_id
rename far_id pnr

merge m:1 pnr using "$work\deaths.dta"
drop if _merge==2

drop d_statdato
rename yod dad_yod

rename death* dad_death*
drop _merge

foreach var of varlist dad_death_CVC-dad_death_COPD {
    replace `var'=0 if `var'==. & pnr!=""
	replace `var'=0 if `var'!=. & pnr!="" & year<dad_yod

	}


** dad medication

preserve

keep pnr year
duplicates drop
drop if pnr==""

merge 1:m pnr year using "$work\lmdb_new_types.dta"
drop if _merge==2
drop _merge


duplicates drop

save "$work\yder_dad_meds.dta", replace

restore


merge m:1 pnr year using "$work\yder_dad_meds.dta"
drop _merge

foreach var of varlist statins ACE metformin COPD_med  {
 	rename `var' dad_`var'
	replace dad_`var'=0 if pnr!="" & dad_`var'==.
	replace dad_`var'=. if pnr==""

}

rename pnr far_id
rename PCP_id pnr


********************************************************************************
********************************************************************************


g PCP_cancer=(mom_death_cancer==1 |dad_death_cancer==1)
tab PCP_cancer
tab year PCP_cancer

g PCP_CVC_deaths=(mom_death_CVC==1 |dad_death_CVC==1)

g PCP_diabets=(mom_metformin==1 | dad_metformin==1 | mom_death_diabetes==1  | dad_death_diabetes==1)
tab PCP_diabets

g PCP_COPD=(mom_COPD_med==1 | dad_COPD_med==1 | mom_death_COPD==1 | dad_death_COPD==1)
tab PCP_COPD

foreach var of varlist PCP_cancer PCP_CVC_deaths PCP_diabets PCP_COPD {
 replace `var'=. if mor_id=="" & far_id==""
}

* Replaceing before the time of death
foreach var of varlist PCP_cancer PCP_CVC_deaths {
 replace `var'=0 if mom_yod>year & mom_yod!=.
 replace `var'=0 if dad_yod>year & dad_yod!=.
}


keep ydernr year PCP_cancer PCP_CVC_deaths PCP_diabets PCP_COPD 

foreach var of varlist PCP_cancer PCP_CVC_deaths PCP_diabets PCP_COPD {
    
	bys ydernr year: egen yder_`var'=max(`var')
    drop `var'
	
}



duplicates drop

save "$work\ydernr_parent_illness.dta", replace




